package chalmers.game.subterranean.model.player;

import chalmers.game.subterranean.model.physics.IMovableBody;
import chalmers.game.subterranean.util.BlockType;
import chalmers.game.subterranean.util.Direction;
import chalmers.game.subterranean.util.options.Speeds;
import lombok.Setter;
import org.apache.log4j.Logger;

/**
 * A player state for handling movements on a ladder.
 * 
 * @author Alex Evert
 */
public class StateLadder implements IState {

  private static final Logger logger = Logger.getLogger("subLog");

  private @Setter
  PlayerContext player;

  /**
   * Perform normal jump and become airbourne.
   * 
   * @param body
   */
  @Override
  public void jump(IMovableBody body) {
    body.setYVelocity(Speeds.JUMPSPEED);
    player.setStateAir();
    logger.info("The player jumped of ladder.");
  }

  /**
   * On a ladder a player can run in any direction.
   * 
   * @param direction the direction the player should run.
   */
  @Override
  public void run(IMovableBody body, Direction direction) {
    if (direction == Direction.EAST) {
      body.setXVelocity(Speeds.RUNSPEED);
      logger.info("The player climbs eastwards.");
    } else if (direction == Direction.WEST) {
      body.setXVelocity(-Speeds.RUNSPEED);
      logger.info("The player climbs westwards.");
    } else if (direction == Direction.NORTH) {
      body.setYVelocity(Speeds.RUNSPEED);
      logger.info("The player climbs north.");
    } else {
      body.setYVelocity(-Speeds.RUNSPEED);
      logger.info("The player climbs south.");
    }
  }

  /**
   * Stop fully.
   */
  @Override
  public void stop(IMovableBody body) {
    body.setXVelocity(0);
    body.setYVelocity(0);
    logger.info("The player stopped on ladder.");
  }

  /**
   * Keep player in square. If the player bounces southward she lands.
   * 
   * @param direction the direction the player bounces against.
   */
  @Override
  public void bounce(IMovableBody body, Direction direction) {
    if (direction == Direction.SOUTH) {
      player.setStateGround();
      logger.info("The player climbed from ladder to solid ground.");
    }
  }

  @Override
  public void checkSurrounding(IMovableBody body) {
    // Check that the player is still on a ladder.
    BlockType block = player.getCurrentBlock();
    if (!block.action.equals(BlockType.Action.LADDER))
      player.setStateAir();
  }
}
